<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>JavaScript模块化(一) | 白黎许</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="./favicon.svg">
    <script src="/blog/utils/js/jq3.5.1.js"></script>
    <script src="/blog/utils/js/click.js"></script>
    <meta name="description" content="挟飞仙以遨游，抱明月而长终">
    <meta name="Keywords" content="白黎许,白黎许的主页,bailixu,BaiLiXu">
    <meta name="author" content="白黎许,白黎许的主页,bailixu,BaiLiXu">
    
    <link rel="preload" href="/blog/assets/css/0.styles.6e9e565f.css" as="style"><link rel="preload" href="/blog/assets/js/app.c63af61e.js" as="script"><link rel="preload" href="/blog/assets/js/3.b175d878.js" as="script"><link rel="preload" href="/blog/assets/js/1.87321680.js" as="script"><link rel="preload" href="/blog/assets/js/13.58ec2c85.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.d598e852.js"><link rel="prefetch" href="/blog/assets/js/11.3b87bb3a.js"><link rel="prefetch" href="/blog/assets/js/12.e489880c.js"><link rel="prefetch" href="/blog/assets/js/14.72724774.js"><link rel="prefetch" href="/blog/assets/js/15.09a90947.js"><link rel="prefetch" href="/blog/assets/js/16.9754d29d.js"><link rel="prefetch" href="/blog/assets/js/17.964079a9.js"><link rel="prefetch" href="/blog/assets/js/18.c30ac25c.js"><link rel="prefetch" href="/blog/assets/js/19.ea852a4f.js"><link rel="prefetch" href="/blog/assets/js/20.fc892859.js"><link rel="prefetch" href="/blog/assets/js/21.2ce787d8.js"><link rel="prefetch" href="/blog/assets/js/22.93931077.js"><link rel="prefetch" href="/blog/assets/js/23.b410d93a.js"><link rel="prefetch" href="/blog/assets/js/24.773fb36d.js"><link rel="prefetch" href="/blog/assets/js/25.f9714bdc.js"><link rel="prefetch" href="/blog/assets/js/26.e7f2f0fc.js"><link rel="prefetch" href="/blog/assets/js/27.bda5cd36.js"><link rel="prefetch" href="/blog/assets/js/28.94bad904.js"><link rel="prefetch" href="/blog/assets/js/29.e0a4dd96.js"><link rel="prefetch" href="/blog/assets/js/30.ecea332e.js"><link rel="prefetch" href="/blog/assets/js/31.b32b5614.js"><link rel="prefetch" href="/blog/assets/js/32.bf89259b.js"><link rel="prefetch" href="/blog/assets/js/33.f84776c8.js"><link rel="prefetch" href="/blog/assets/js/34.970968f0.js"><link rel="prefetch" href="/blog/assets/js/35.9cf485c7.js"><link rel="prefetch" href="/blog/assets/js/4.9e8d3162.js"><link rel="prefetch" href="/blog/assets/js/5.0e652f79.js"><link rel="prefetch" href="/blog/assets/js/6.82e85603.js"><link rel="prefetch" href="/blog/assets/js/7.ec2c34e4.js"><link rel="prefetch" href="/blog/assets/js/8.da93c643.js"><link rel="prefetch" href="/blog/assets/js/9.4b7dc97f.js">
    <link rel="stylesheet" href="/blog/assets/css/0.styles.6e9e565f.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-1156296a><div data-v-1156296a><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-1156296a data-v-1156296a><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-4e82dffc data-v-1156296a data-v-1156296a><h3 class="title" data-v-4e82dffc data-v-4e82dffc>白黎许</h3> <p class="description" data-v-4e82dffc data-v-4e82dffc>挟飞仙以遨游，抱明月而长终</p> <label id="box" class="inputBox" data-v-4e82dffc data-v-4e82dffc><input type="password" value="" data-v-4e82dffc> <span data-v-4e82dffc>Konck! Knock!</span> <button data-v-4e82dffc>OK</button></label> <div class="footer" data-v-4e82dffc data-v-4e82dffc><span data-v-4e82dffc><i class="iconfont reco-theme" data-v-4e82dffc></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-4e82dffc>vuePress-theme-reco</a></span> <span data-v-4e82dffc><i class="iconfont reco-copyright" data-v-4e82dffc></i> <a data-v-4e82dffc><span data-v-4e82dffc>白黎许</span>
            
          <span data-v-4e82dffc>2021 - </span>
          2022
        </a></span></div></div> <div class="hide" data-v-1156296a><header class="navbar" data-v-1156296a><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><img src="/blog/images/head_portrait.jpg" alt="白黎许" class="logo"> <span class="site-name">白黎许</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/JavaScript/" class="nav-link"><i class="undefined"></i>
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/Node.js/" class="nav-link"><i class="undefined"></i>
  Node.js
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/Vue/" class="nav-link"><i class="undefined"></i>
  Vue
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont icon-tools"></i>
      工具箱
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://www.bilibili.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="undefined"></i>
  哔哩哔哩
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://bigjpg.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="undefined"></i>
  图片放大
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://www.tiobe.com/tiobe-index/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="undefined"></i>
  编程语言排行榜
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://www.bootcdn.cn/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="undefined"></i>
  BootCdn猫云
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont icon-tools"></i>
      项目
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://coderblx.gitee.io/mall" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont icon-shop"></i>
  白黎许的购物商场(移动端)
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="http://coderblx.gitee.io/backend" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont icon-shop"></i>
  白黎许的后台管理系统(PC端)
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div><div class="nav-item"><a href="/blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><a href="/blog/views/others/message.html" class="nav-link"><i class="iconfont reco-message"></i>
  留言板
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      关于
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/coderblx" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://github.com/coderblx" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  Github
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-1156296a></div> <aside class="sidebar" data-v-1156296a><div class="personal-info-wrapper" data-v-828910c6 data-v-1156296a><img src="/blog/images/head_portrait.jpg" alt="author-avatar" class="personal-img" data-v-828910c6> <h3 class="name" data-v-828910c6>
    白黎许
  </h3> <div class="num" data-v-828910c6><div data-v-828910c6><h3 data-v-828910c6>23</h3> <h6 data-v-828910c6>Articles</h6></div> <div data-v-828910c6><h3 data-v-828910c6>11</h3> <h6 data-v-828910c6>Tags</h6></div></div> <ul class="social-links" data-v-828910c6></ul> <hr data-v-828910c6></div> <nav class="nav-links"><div class="nav-item"><a href="/blog/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/JavaScript/" class="nav-link"><i class="undefined"></i>
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/Node.js/" class="nav-link"><i class="undefined"></i>
  Node.js
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/Vue/" class="nav-link"><i class="undefined"></i>
  Vue
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont icon-tools"></i>
      工具箱
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://www.bilibili.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="undefined"></i>
  哔哩哔哩
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://bigjpg.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="undefined"></i>
  图片放大
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://www.tiobe.com/tiobe-index/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="undefined"></i>
  编程语言排行榜
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://www.bootcdn.cn/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="undefined"></i>
  BootCdn猫云
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont icon-tools"></i>
      项目
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="http://coderblx.gitee.io/mall" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont icon-shop"></i>
  白黎许的购物商场(移动端)
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="http://coderblx.gitee.io/backend" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont icon-shop"></i>
  白黎许的后台管理系统(PC端)
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div><div class="nav-item"><a href="/blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><a href="/blog/views/others/message.html" class="nav-link"><i class="iconfont reco-message"></i>
  留言板
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      关于
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/coderblx" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://github.com/coderblx" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  Github
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-4e82dffc data-v-1156296a><h3 class="title" data-v-4e82dffc data-v-4e82dffc>JavaScript模块化(一)</h3> <!----> <label id="box" class="inputBox" data-v-4e82dffc data-v-4e82dffc><input type="password" value="" data-v-4e82dffc> <span data-v-4e82dffc>Konck! Knock!</span> <button data-v-4e82dffc>OK</button></label> <div class="footer" data-v-4e82dffc data-v-4e82dffc><span data-v-4e82dffc><i class="iconfont reco-theme" data-v-4e82dffc></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-4e82dffc>vuePress-theme-reco</a></span> <span data-v-4e82dffc><i class="iconfont reco-copyright" data-v-4e82dffc></i> <a data-v-4e82dffc><span data-v-4e82dffc>白黎许</span>
            
          <span data-v-4e82dffc>2021 - </span>
          2022
        </a></span></div></div> <div data-v-1156296a><main class="page"><section><div class="page-title"><h1 class="title">JavaScript模块化(一)</h1> <div data-v-1ff7123e><i class="iconfont reco-account" data-v-1ff7123e><span data-v-1ff7123e>白黎许</span></i> <i class="iconfont reco-date" data-v-1ff7123e><span data-v-1ff7123e>1/22/2022</span></i> <i class="iconfont reco-eye" data-v-1ff7123e><span id="/blog/views/Node/CommonJS.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-1ff7123e><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="tags iconfont reco-tag" data-v-1ff7123e><span class="tag-item" data-v-1ff7123e>JavaScript</span><span class="tag-item" data-v-1ff7123e>Node</span></i></div></div> <div class="theme-reco-content content__default"><div class="custom-block tip"><p class="title"></p><p>了解JS中的模块化规范：CommonJS、AMD、CMD</p></div> <h2 id="一、什么是模块化"><a href="#一、什么是模块化" class="header-anchor">#</a> 一、什么是模块化</h2> <ul><li>事实上模块化开发最终的目的是将程序划分成<strong>一个个小的结构</strong></li> <li>这个结构中编写属于<strong>自己的逻辑代码</strong>，有<strong>自己的作用域</strong>，不会影响到其他的结构</li> <li>这个结构可以将自己希望暴露的<strong>变量、函数、对象等导出</strong>给其结构使用</li> <li>也可以通过某种方式，导入另外结构中的<strong>变量、函数、对象</strong>等</li></ul> <p>上面说提到的<strong>结构</strong>，就是<strong>模块</strong>;按照这种<strong>结构划分</strong>开发程序的过程，就是<strong>模块化开发</strong>的过程</p> <p>然而，无论你多么喜欢JavaScript，以及它现在发展的有多好，我们都需要承认在<em>Brendan Eich</em>用了10天写出JavaScript的时候，它都有很多的缺陷︰</p> <ul><li>比如var定义的变量作用域问题;</li> <li>比如JavaScript的面向对象并不能像常规面向对象语言一样使用class;</li> <li>比如JavaScript没有模块化的问题;</li></ul> <p>但是，随着前端和JavaScript的快速发展，模块化已经是JavaScript一个非常迫切的需求</p> <p>直到<strong>ES6</strong>(2015)，JavaScript终于推出了自己的模块化解决方案，在此之前，为了让JavaScript支持模块化，涌现除了很多不同的模块化规范：<strong>AMD、CMD、CommonJS</strong>等</p> <h2 id="二、commonjs与node"><a href="#二、commonjs与node" class="header-anchor">#</a> 二、CommonJS与Node</h2> <p>我们知道<strong>CommonJS</strong>是一个规范，最初提出来是在浏览器以外的地方使用，并且当时被命名为<strong>ServerJS</strong>，后来为了体现它的广泛性，修改为<strong>CommonJS</strong></p> <p>在Node中，对CommonJS进行了支持和实现，让我们在开发Node的过程中可以方便的进行模块化开发：</p> <ul><li>在Node中每一个js文件都是<strong>一个单独的模块</strong></li> <li>这个模块中包括CommonJS规范的核心变量：<code>exports</code>、<code>module.exports</code>、<code>require</code></li> <li>我们可以使用这些变量来方便的进行<strong>模块化开发</strong></li></ul> <p><code>exports</code>和<code>module.exports</code>可以负责对模块中的内容进行导出</p> <p><code>require</code>函数可以帮助我们导入其他模块(自定义模块、系统模块、第三方库模块)中的内容</p> <h3 id="_2-1-exports与require"><a href="#_2-1-exports与require" class="header-anchor">#</a> 2.1 exports与require</h3> <p>我们通过一段两个文件中的代码来体验一下<code>exports</code>与<code>require</code>的作用</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// bar.js</span>
<span class="token keyword">const</span> myName <span class="token operator">=</span> <span class="token string">'Blx'</span>
<span class="token keyword">const</span> age <span class="token operator">=</span> <span class="token number">18</span>

<span class="token keyword">let</span> Info <span class="token operator">=</span> <span class="token string">'my name is Blx'</span>

<span class="token keyword">function</span> <span class="token function">sayHello</span><span class="token punctuation">(</span><span class="token parameter">name</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'Hello '</span> <span class="token operator">+</span> name<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// exports是Node内部的一个默认对象 默认为空对象</span>
exports<span class="token punctuation">.</span>myName <span class="token operator">=</span> myName
exports<span class="token punctuation">.</span>age <span class="token operator">=</span> age
exports<span class="token punctuation">.</span>sayHello <span class="token operator">=</span> sayHello
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// main.js</span>
<span class="token comment">// require返回的就是exports对象</span>
<span class="token keyword">const</span> bar <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./bar'</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>bar<span class="token punctuation">.</span>myName<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Blx</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>bar<span class="token punctuation">.</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 18</span>
bar<span class="token punctuation">.</span><span class="token function">sayHello</span><span class="token punctuation">(</span><span class="token string">'我是白黎许'</span><span class="token punctuation">)</span> <span class="token comment">//Hello 我是白黎许</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>可以看到，通过<code>exports</code>从<code>bar.js</code>中导出需要的内容后，在<code>main.js</code>中通过调用<code>require</code>函数导入了我们需要的内容，并且成功的打印出了内容</p> <p>本质上就是通过<code>require</code>将<code>exports</code>对象的<strong>内存地址赋值给了某一个属性</strong>，我们通过代码来验证一下：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// bar.js</span>
<span class="token keyword">const</span> myName <span class="token operator">=</span> <span class="token string">'Blx'</span>
<span class="token keyword">const</span> age <span class="token operator">=</span> <span class="token number">18</span>

<span class="token keyword">let</span> Info <span class="token operator">=</span> <span class="token string">'my name is Blx'</span>

<span class="token keyword">function</span> <span class="token function">sayHello</span><span class="token punctuation">(</span><span class="token parameter">name</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'Hello '</span> <span class="token operator">+</span> name<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// exports是Node内部的一个默认对象 默认为空对象</span>
exports<span class="token punctuation">.</span>myName <span class="token operator">=</span> myName
exports<span class="token punctuation">.</span>age <span class="token operator">=</span> age
exports<span class="token punctuation">.</span>sayHello <span class="token operator">=</span> sayHello

<span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>exports<span class="token punctuation">.</span>test<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 我会出现在bar.js的exports对象中~</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">2000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// main.js</span>
<span class="token comment">// require返回的就是exports对象</span>
<span class="token keyword">const</span> bar <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./bar'</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>bar<span class="token punctuation">.</span>myName<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Blx</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>bar<span class="token punctuation">.</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 18</span>
bar<span class="token punctuation">.</span><span class="token function">sayHello</span><span class="token punctuation">(</span><span class="token string">'我是白黎许'</span><span class="token punctuation">)</span> <span class="token comment">//Hello 我是白黎许</span>

<span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  bar<span class="token punctuation">.</span>test <span class="token operator">=</span> <span class="token string">'我会出现在bar.js的exports对象中~'</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>可以看到，通过Node运行<code>main.js</code>后，在控制台延迟2秒后，打印了并未在<code>bar.js</code>中定义过的属性<code>test</code>的值</p> <p>这也说明了<code>require</code>函数确实是将<code>exports</code>对象的<strong>内存地址赋值给了某一个属性</strong>，它们实际上是一个<strong>浅层拷贝</strong></p> <h3 id="_2-2-module-exports与exports的关系与区别"><a href="#_2-2-module-exports与exports的关系与区别" class="header-anchor">#</a> 2.2 module.exports与exports的关系与区别</h3> <p>在Node中我们经常导出内容时，又是通过<code>module.exports</code>导出的</p> <p>那么<code>module.exports</code>与<code>exports</code>又有什么关系与区别呢？</p> <p>我们追根溯源，通过维基百科中对CommonJS规范的解析：</p> <ul><li>CommonJS中是<strong>没有</strong><code>module.exports</code>的概念的</li> <li>但是为了实现模块的导出，Node中使用的是<code>Module</code>的类，<strong>每一个模块都是Module的一个实例</strong>，也就是<code>module</code>模块对象</li> <li>所以在Node中真正用于导出的其实根本不是<code>exports</code>，而是<code>module.exports</code></li> <li>因为<code>module</code>模块对象才是导出的<strong>真正实现者</strong></li></ul> <blockquote><p>Node中存在<code>exports</code>其实是为了满足CommonJS规范而做出的一种妥协</p></blockquote> <p>那为什么我们往<code>exports</code>对象上添加的属性和方法本质上却是由<code>module.exports</code>进行导出的呢？</p> <p>其实非常简单，Node的源码里帮我们做了一件事情：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>module<span class="token punctuation">.</span>exports <span class="token operator">=</span> exports
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>其实就是对<code>exports</code>对象进行了一次浅拷贝，将<code>exports</code>对象的内存地址赋值给了<code>module.exports</code>的</p> <p>也就是说<code>module.exports === exports === main.js中的bar</code></p> <p>我们通过一个小案例来验证一下：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// bar.js</span>
<span class="token keyword">const</span> myName <span class="token operator">=</span> <span class="token string">'Blx'</span>
<span class="token keyword">const</span> age <span class="token operator">=</span> <span class="token number">18</span>

<span class="token comment">// exports是Node内部的一个默认对象 默认为空对象</span>
exports<span class="token punctuation">.</span>myName <span class="token operator">=</span> myName
exports<span class="token punctuation">.</span>age <span class="token operator">=</span> age

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>module<span class="token punctuation">.</span>exports<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// { myName: 'Blx', age: 18 }</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>exports<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// { myName: 'Blx', age: 18 }</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>module<span class="token punctuation">.</span>exports <span class="token operator">===</span> exports<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// main.js</span>
<span class="token comment">// require返回的就是exports对象</span>
<span class="token keyword">const</span> bar <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./bar'</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>bar<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// { myName: 'Blx', age: 18 }</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>可以看出来，<code>module.exports</code>和<code>exports</code>确实是指向的<strong>同一个内存地址，可以说他们是同一个对象</strong></p> <p>所以我们在导出内容时，可以直接将一个对象赋值给<code>module.exports</code>进行导出</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// bar.js</span>
<span class="token keyword">const</span> myName <span class="token operator">=</span> <span class="token string">'Blx'</span>
<span class="token keyword">const</span> age <span class="token operator">=</span> <span class="token number">18</span>

<span class="token comment">// 本质上是module.exports在导出</span>
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
  myName<span class="token punctuation">,</span>
  age
<span class="token punctuation">}</span>
<span class="token comment">// 注意！！这样引用后，module.exports不再和exports指向同一个对象</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>module<span class="token punctuation">.</span>exports <span class="token operator">===</span> exports<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//false</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// main.js</span>
<span class="token comment">// require返回的就是module.exports引用的那个对象(不是exports对象了)</span>
<span class="token keyword">const</span> bar <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./bar'</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>bar<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// { myName: 'Blx', age: 18 }</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>总结：</p> <ul><li>默认情况下，Node内部<code>module.exports = exports</code></li> <li>但是一旦<strong>修改了</strong><code>module.exports</code>引用的对象，则<code>module.exports</code>和<code>exports</code>对象不存在任何关系，他们<strong>分别指向不同的对象的内存地址</strong></li></ul> <h3 id="_2-3-require细节"><a href="#_2-3-require细节" class="header-anchor">#</a> 2.3 require细节</h3> <p><code>require</code>是一个函数，可以帮助我们引入一个文件（模块）中导入的对象</p> <p>导入格式为<code>require(X)</code>时<code>require</code>的查找规则：</p> <ol><li>X是一个核心模块，比如<code>path</code>、<code>http</code> <ul><li>直接返回<strong>核心模块</strong>，并且<strong>停止查找</strong></li></ul></li> <li>X是以<code>./</code>或<code>../</code>或<code>/</code>(根目录)开头的
<ul><li>将X当作一个文件在对应的目录下查找
<ul><li>如果X有后缀名，按照后缀名的格式查找对应的文件</li> <li>如果没有后缀名，会按以下顺序查找：
<ol><li>直接查找X文件</li> <li>查找X.js文件</li> <li>查找X.json文件</li> <li>查找X.node文件</li></ol></li></ul></li> <li>没有找到对应的文件，将X作为一个目录
<ul><li>查找目录下面的index文件
<ol><li>查找X/index.js文件</li> <li>查找X/index.json文件</li> <li>查找X/index.node文件</li></ol></li></ul></li> <li>如果没有找到，那么报错：<code>not found</code></li></ul></li> <li>直接是一个X(没有路径)，但X不是一个核心模块
<ul><li>一层一层的从<code>node_modules</code>文件夹中查找</li> <li>如果所有层级中都没有<code>node_modules</code>文件夹或者<code>node_modules</code>文件夹内没有找到对应文件，那么会报错：<code>not found</code></li></ul></li></ol> <h3 id="_2-4-模块的加载过程"><a href="#_2-4-模块的加载过程" class="header-anchor">#</a> 2.4 模块的加载过程</h3> <ol><li>模块在<strong>第一次</strong>被引入时，模块中的js代码会被执行一次</li> <li>模块被多次引入时，会缓存，最终只加载（运行）一次
<ul><li>为什么只会加载运行一次呢？</li> <li>这是因为每个模块对象<code>module</code>都有一个属性：<code>loaded</code></li> <li>为<code>false</code>表示还没有加载，为<code>true</code>表示已经加载</li></ul></li> <li>如果有循环引用，那么加载顺序是什么？
<ul><li>这其实是一种数据结构：图结构</li> <li>图结构在遍历的过程中，有<strong>深度优先搜索</strong>和<strong>广度优先搜索</strong></li> <li>Node采用的是<strong>深度优先算法</strong>: main &gt; aaa &gt; ccc &gt; ddd &gt; eee &gt; bbb</li></ul></li></ol> <p><img src="/blog/assets/img/circular-reference.34ab202b.png" alt="循环引用"></p> <h3 id="_2-5-commonjs规范缺点"><a href="#_2-5-commonjs规范缺点" class="header-anchor">#</a> 2.5 CommonJS规范缺点</h3> <ol><li>CommonJS加载模块是同步的：
<ul><li>同步的意味着只有等到对应的模块加载完毕，当前模块中的内容才能被运行</li> <li>这在服务器中并不会有什么问题，因为服务器加载的js文件都是本地文件，加载速度非常快</li></ul></li> <li>如果将它应用于浏览器呢？
<ul><li>浏览器加载js文件需要先从服务器将文件下载下来，之后在加载运行</li> <li>那么采用同步的就意味着后续的js代码都无法正常运行，即使是一些简单的DOM操作</li></ul></li></ol> <h2 id="三、amd规范"><a href="#三、amd规范" class="header-anchor">#</a> 三、AMD规范</h2> <p>AMD主要是应用于浏览器的一种模块化规范：</p> <ul><li>AMD是Asynchronous Module Definition（异步模块定义）的缩写</li> <li>它采用的是异步加载模块</li> <li>事实上AMD的规范还要早于CommonJS，但是CommonJS目前依然在被使用，而AMD使用的较少了</li></ul> <h2 id="四、cmd"><a href="#四、cmd" class="header-anchor">#</a> 四、CMD</h2> <p>CMD规范也是应用于浏览器的一种模块化规范：</p> <ul><li>CMD是Common Module Definition (通用模块定义)的缩写</li> <li>它也采用了异步加载模块，但是它将CommonJS的优点吸收了过来</li> <li>但是目前CMD使用也非常少了</li></ul></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">最后更新于: </span> <span class="time">2022年2月28日星期一凌晨12点13分</span></div></footer> <!----> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-70334359><li class="level-2" data-v-70334359><a href="/blog/views/Node/CommonJS.html#一、什么是模块化" class="sidebar-link reco-side-一、什么是模块化" data-v-70334359>一、什么是模块化</a></li><li class="level-2" data-v-70334359><a href="/blog/views/Node/CommonJS.html#二、commonjs与node" class="sidebar-link reco-side-二、commonjs与node" data-v-70334359>二、CommonJS与Node</a></li><li class="level-3" data-v-70334359><a href="/blog/views/Node/CommonJS.html#_2-1-exports与require" class="sidebar-link reco-side-_2-1-exports与require" data-v-70334359>2.1 exports与require</a></li><li class="level-3" data-v-70334359><a href="/blog/views/Node/CommonJS.html#_2-2-module-exports与exports的关系与区别" class="sidebar-link reco-side-_2-2-module-exports与exports的关系与区别" data-v-70334359>2.2 module.exports与exports的关系与区别</a></li><li class="level-3" data-v-70334359><a href="/blog/views/Node/CommonJS.html#_2-3-require细节" class="sidebar-link reco-side-_2-3-require细节" data-v-70334359>2.3 require细节</a></li><li class="level-3" data-v-70334359><a href="/blog/views/Node/CommonJS.html#_2-4-模块的加载过程" class="sidebar-link reco-side-_2-4-模块的加载过程" data-v-70334359>2.4 模块的加载过程</a></li><li class="level-3" data-v-70334359><a href="/blog/views/Node/CommonJS.html#_2-5-commonjs规范缺点" class="sidebar-link reco-side-_2-5-commonjs规范缺点" data-v-70334359>2.5 CommonJS规范缺点</a></li><li class="level-2" data-v-70334359><a href="/blog/views/Node/CommonJS.html#三、amd规范" class="sidebar-link reco-side-三、amd规范" data-v-70334359>三、AMD规范</a></li><li class="level-2" data-v-70334359><a href="/blog/views/Node/CommonJS.html#四、cmd" class="sidebar-link reco-side-四、cmd" data-v-70334359>四、CMD</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div><!----><div class="kanbanniang" data-v-5775ee02><div class="banniang-container" style="display:;" data-v-5775ee02><div class="messageBox" style="right:68px;bottom:190px;display:none;" data-v-5775ee02>
      心里的花，我想要带你回家。
    </div> <div class="operation" style="right:90px;bottom:40px;display:;" data-v-5775ee02><i class="kbnfont kbn-ban-home ban-home" data-v-5775ee02></i> <i class="kbnfont kbn-ban-message message" data-v-5775ee02></i> <i class="kbnfont kbn-ban-close close" data-v-5775ee02></i> <a target="_blank" href="https://vuepress-theme-reco.recoluan.com/views/plugins/kanbanniang.html" data-v-5775ee02><i class="kbnfont kbn-ban-info info" data-v-5775ee02></i></a> <i class="kbnfont kbn-ban-theme skin" style="display:;" data-v-5775ee02></i></div> <canvas id="banniang" width="150" height="219" class="live2d" style="right:90px;bottom:-20px;opacity:0.9;" data-v-5775ee02></canvas></div> <div class="showBanNiang" style="display:none;" data-v-5775ee02>
    看板娘
  </div></div><div></div><APlayer audio="" fixed="true" mini="true" autoplay="autoplay" theme="#b7daff" loop="loop" order="random" preload="auto" volume="0.5" mutex="true" lrc-type="3" list-folded="true" list-max-height="250" storage-name="vuepress-plugin-meting" id="aplayer-fixed"></APlayer><!----><div class="RibbonAnimation"></div></div></div>
    <script src="/blog/assets/js/app.c63af61e.js" defer></script><script src="/blog/assets/js/3.b175d878.js" defer></script><script src="/blog/assets/js/1.87321680.js" defer></script><script src="/blog/assets/js/13.58ec2c85.js" defer></script>
  </body>
</html>
